package com.hc.service.impl;

import com.hc.annotation.DataDict;
import com.hc.dao.IDataDictDAO;
import com.hc.entity.TDict;
import com.hc.service.IDataDictService;
import com.hc.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Service
public class DataDictImpl implements IDataDictService {

    IDataDictDAO dataDictDAO;

    @Override
    public boolean checkIfKeyExist(Object entity) {
        try {
            iterateNotNullFields(entity, ((tableName, colName, scope, field, ent) -> {
                if (dataDictDAO.countByColNameTableNameAndKey(colName, tableName, (String) field.get(entity)) <= 0) {
                    throw new ClassCastException();
                }
            }));
            return true;
        } catch (IllegalAccessException | ClassCastException e) {
            return false;
        }
    }

    interface Iterator {
        void callback(String tableName, String colName, String scope, Field field, Object ent) throws IllegalAccessException,ClassCastException;
    }


    private static void iterateNotNullFields(Object entity, Iterator iterator) throws IllegalAccessException, ClassCastException {
        Class<?> table = entity.getClass();
        String tableName = StringUtil.camelCaseToUnderLine(table.getSimpleName());
        String colName;
        Object key;
        String scope = "DB";
        for (Field field : table.getDeclaredFields()) {
            if (field.isAnnotationPresent(DataDict.class)) {
                field.setAccessible(true);
                colName = StringUtil.camelCaseToUnderLine(field.getName());
                key = field.get(entity);
                if (key instanceof String) {
                    iterator.callback(tableName, colName, scope, field, entity);
                } else {
                    System.out.println("类" + table.getName() + "需要查表的字段" + colName + "不是string类型或不存在");
                }
            }
        }
    }

    @Autowired
    public DataDictImpl(IDataDictDAO dataDictDAO) {
        this.dataDictDAO = dataDictDAO;
    }




    @Override
    public void fillValByDictIfExist(Object entity) {
        try {
            iterateNotNullFields(entity, (tableName, colName, scope, field, ent) -> {
                List<String> result;
                if ((result = dataDictDAO.getStatusCodeByColNameAndTableName(colName, tableName, (String) field.get(entity))).size() > 0) {
                    field.set(ent, result.get(0));
                }
            });
        } catch (IllegalAccessException | ClassCastException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<TDict> listByTableNameColName(String tableName, String colName) {
        List<TDict> data = dataDictDAO.getAllByTableNameAndColName(colName, tableName);
        if (data == null) {
            return new ArrayList<>();
        }
        return data;
    }
}
